<?php

namespace App\Repository\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Redis;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;

/**
 * Class PaymentOrder.
 *
 * @package namespace App\Repository\Models;
 */
class PaymentOrder extends Model implements Transformable
{
    use TransformableTrait;
    use SoftDeletes;

    protected $table = 'payment_order';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['user_id', 'channel', 'trade_no', 'order_codes', 'order_money', 'pay_money', 'pay_status', 'paytime', 'trade_buyer_id', 'trade_buyer_email', 'external_trade_no', 'payment_type'];

    public static $channel_map = [
        '0' => '浙商网关',
        '1' => '浙商快捷'
    ];

    const PAYMENT_TYPE_PAY = 1; //支付
    const PAYMENT_TYPE_TOP_UP = 2;  //提现

    const UNPAID = 0;
    const PREPAID = 1;

    const PAY_STATUS_UNPAID = 0;
    const PAY_STATUS_PREPAID = 1;

    public static function boot()
    {
        self::creating(function ($order) {
            //流水号: C年(后2位)+月+日+店铺id（6位，前置补0）+8位流水 （如18031500000001）
            $shop_payment_order_serial = Redis::INCR('order:payment_order:serial') % 100000000;
            $shop_payment_order_serial_fill = sprintf('%08s', $shop_payment_order_serial);
            $order->trade_no = 'C' . date('ymd') . $shop_payment_order_serial_fill;
        });
        parent::boot(); // TODO: Change the autogenerated stub
    }

}
